package com.shoe.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;

import com.shoe.pojo.ShoeGood;
import com.shoe.pojo.ShoeOrder;
import com.shoe.service.ShoeGoodsService;
import com.shoe.service.ShoeOrderService;
import com.shoe.service.UserLogisticService;
import com.shoe.util.AppUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@CrossOrigin
public class PayController {
    @Autowired
    private AlipayClient alipayClient;
    @Autowired
    private AlipayTradePagePayRequest alipayTradePagePayRequest;

    @Autowired
    ShoeGoodsService shoeGoodsService ;

    @Autowired
    ShoeOrderService shoeOrderService;

    @Autowired
    UserLogisticService userLogisticService;


    //处理支付请求
    //1.接收页面传过来的数据:订单号，金额，名称，商品描述  表单中的name值=参数名
    @RequestMapping("pay")
    public void pay(HttpServletResponse response,String WIDout_trade_no, String WIDsubject, String WIDtotal_amount, String WIDbody,String goodSize,String goodId,String goodCounts,String userId)
         throws AlipayApiException, IOException {
        //2.获得支付的客户端AlipayClient,和配置支付信息的对象AlipayTradePagePayRequest
        //3.设置响应的地址(支付宝返回给商户的响应地址)
//        int a = Integer.parseInt(orderMoney);

        if(userId != null && goodId != null && goodCounts != null && goodSize != null){
            System.out.println(userId);
            System.out.println(goodId);
            System.out.println(goodCounts);
            System.out.println(goodSize);
            System.out.println(WIDbody);
            System.out.println(WIDout_trade_no);
            String[] a = goodId.split(",");
            String[] b = goodSize.split(",");
            String[] c = goodCounts.split(",");
            ShoeOrder shoeOrder1 = new ShoeOrder();
            shoeOrder1.setOrderNo(WIDout_trade_no);
            int x = shoeOrderService.selectByNo(shoeOrder1);
            if(x == 0){
                for (int i=0 ;i< a.length;i++){
                    ShoeGood shoeGood = shoeGoodsService.selectById(Long.valueOf(a[i]));
                    Long money=shoeGood.getGoodMoney();
                    ShoeOrder shoeOrder = new ShoeOrder();
                    shoeOrder.setOrderNo(WIDout_trade_no);
                    shoeOrder.setOrderMoney(Long.valueOf(WIDtotal_amount));
                    shoeOrder.setOrderUserId(Long.valueOf(userId));
                    shoeOrder.setOrderGoodId(Long.valueOf(a[i]));
                    shoeOrder.setOrderCount(Byte.valueOf(c[i]));
                    shoeOrder.setReserve3(b[i]);
                    Date d = new Date();
                    shoeOrder.setOrderDate(d);
                    shoeOrder.setOrderState("未付款");
                    shoeOrder.setReserve2(WIDbody);
                    shoeOrderService.addOrder(shoeOrder);
                }
            }else {
                response.sendRedirect("http://localhost:63343/qiguai/html/order.html?1");
            }
        }

        System.out.println(WIDbody);
        System.out.println(WIDout_trade_no);
        System.out.println(WIDsubject);
        System.out.println(WIDtotal_amount);

        /*ShoeOrder shoeOrder2 = new ShoeOrder();
        shoeOrder2.setOrderNo(WIDout_trade_no);
        long sum =0;*/
        //判断是否存在订单，如果有订单，找到订单一起结账；
        /*List<ShoeOrder> list = shoeOrderService.selectByNo2(shoeOrder2);
        if(list != null && list.size()>0){
            for (int t=0 ; t<list.size();t++){
              int y =   list.get(t).getOrderCount();
              Long z =  list.get(t).getOrderMoney();
              Long u = y*z;
              sum = sum + u;
            }
            System.out.println(sum);
            WIDtotal_amount = String.valueOf(sum);
        }*/
/*

        List<Long> list1 = new ArrayList<>();
        for (int i =0 ;i<list.size();i++){
            list1.add(list.get(i).getOrderId());
        }
        userLogisticService.addOrderIdList(list1);
*/



        alipayTradePagePayRequest.setNotifyUrl(AppUtil.notify_url);
        alipayTradePagePayRequest.setReturnUrl(AppUtil.return_url);
        //4.设置请求的参数(传递给支付宝的数据)
        alipayTradePagePayRequest.setBizContent(
                "{\"out_trade_no\":\""+ WIDout_trade_no +"\","
                        + "\"total_amount\":\""+ WIDtotal_amount +"\","
                        + "\"subject\":\""+ WIDsubject +"\","
                        + "\"body\":\""+ WIDbody +"\","
                        + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        //5.发送请求
        String result = alipayClient.pageExecute(alipayTradePagePayRequest).getBody();
        //6.将响应结果返回给支付宝前端



            response.setContentType("text/html;charset=utf-8");
            response.getWriter().println(result);


       /* ShoeOrder shoeOrder = new ShoeOrder();
        shoeOrder.setOrderUserId(Long.valueOf(userId));
        shoeOrder.setOrderGoodId(Long.valueOf(goodId));
        shoeOrder.setOrderState("未发货");

//        shoeOrder.setOrderMoney( Long.valueOf(orderMoney));
        shoeOrder.setOrderCount(Byte.valueOf(goodCounts));*/


    }
}
